package com.easychat.controller.Admin;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.controller.ABaseController;
import com.easychat.entity.po.AppUpdate;
import com.easychat.entity.query.AppUpdateQuery;
import com.easychat.entity.vo.PaginationResultVO;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.service.AppUpdateService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * @Author Dongdong
 * @注释 用户联系人
 */
@RestController("adminAppUpdateController")
@RequestMapping("/admin")
public class AdminAppUpdateController extends ABaseController {

    @Resource
    private AppUpdateService appUpdateService;

    @RequestMapping("/loadUpdateList")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO loadUpdateList(AppUpdateQuery appUpdateQuery) {
        appUpdateQuery.setOrderBy("create_time desc");
        PaginationResultVO<AppUpdate> resultVo = appUpdateService.findListByPage(appUpdateQuery);

//        List<AppUpdate> updateList = resultVo.getList();
//        for (AppUpdate appUpdate : updateList) {
//            appUpdate.setUpdateDescArray(appUpdate.getUpdateDesc().split("\\|"));
//        }
        return getSuccessResponseVO(resultVo);

    }

    @RequestMapping("/saveUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO saveUpdate(String updateDesc,
                                 String outerLink,
                                 Integer id,
                                 @NotEmpty Integer fileType,
                                 @NotEmpty MultipartFile file,
                                 @NotEmpty String version) throws IOException {
        AppUpdate appUpdate = new AppUpdate();
        appUpdate.setId(id);
        appUpdate.setVersion(version);
        appUpdate.setOuterLink(outerLink);
        appUpdate.setFileType(fileType);
        appUpdate.setUpdateDesc(updateDesc);

        //进入方法前，已经将文件上传到服务器了！这是springboot的特性！
        appUpdateService.saveUpdate(appUpdate, file);
        return getSuccessResponseVO(null);

    }

    @RequestMapping("/delUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO delUpdate(@NotNull Integer id) throws IOException {
        appUpdateService.deleteAppUpdateById(id);
        return getSuccessResponseVO(null);

    }

    @RequestMapping("/postUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO postUpdate( @NotNull Integer id
                                  , @NotEmpty String status
                                  , @NotEmpty String grayscaleUid) throws IOException {
        appUpdateService.postUpdate(id, status, grayscaleUid);
        return getSuccessResponseVO(null);

    }
}
